package com.beamfield.controller.manage.steelplant;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.ProcDadoGroup;
import com.beamfield.model.SteelplantUsepartRebar;
import com.beamfield.service.manage.sys.SuppliesService;
import com.beamfield.service.serviceimpl.manage.steelplant.SteelplantSettingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Zephyr
 * @description:
 * @date 2019/7/17
 */
@Controller
@RequestMapping("/cms/steelplant/setting")
public class SettingController extends BaseController {

    @Autowired
    private SteelplantSettingService steelplantSettingService;

    @Autowired
    private SuppliesService suppliesService;

    @RequestMapping("/usepart")
    public ModelAndView usepart(){
        return new ModelAndView();
    }

    @RequestMapping("/usepart_rebar")
    public ModelAndView usepartRebar(){
        return new ModelAndView();
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUsepartType", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getUsepartType(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        try {
            return this.steelplantSettingService.getUsepartType(beamId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSaveUsepartType", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSaveUsepartType(HttpServletRequest request,
                                    @RequestParam("typeName") String typeName,
                                    @RequestParam("typeId") String typeId){
        if(StringUtils.isEmpty(typeName)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        try{
            return this.steelplantSettingService.saveUsepartType(typeName, typeId, beamId, userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteUsepartType", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteUsepartType(HttpServletRequest request, @RequestParam("id") String id){
        if(StringUtils.isEmpty(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return this.steelplantSettingService.deleteUsepartType(id);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/readUsepart",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readUsepart(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String typeId = request.getParameter("typeId");
        return steelplantSettingService.getUsepart(typeId, offset, limit);
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSaveUsepart", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSaveUsepart(HttpServletRequest request,
                                       @RequestParam("usepartName") String usepartName,
                                       @RequestParam(value = "typeId", required = false) Long typeId,
                                       @RequestParam(value = "id", required = false) String id){
        if(StringUtils.isEmpty(usepartName)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        try{
            return this.steelplantSettingService.saveUsepart(typeId, usepartName, id, beamId, userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteUsepart", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteUsepart(HttpServletRequest request, @RequestParam("id") String id){
        if(StringUtils.isEmpty(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return this.steelplantSettingService.deleteUsepart(id);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }


    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/readUsepartRebar",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readUsepartRebar(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String usepartId = request.getParameter("usepartId");
        return steelplantSettingService.getUsepartRebar(usepartId, offset, limit);
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSaveUsepartRebar", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSaveUsepartRebar(HttpServletRequest request, SteelplantUsepartRebar steelplantUsepartRebar){
        if(steelplantUsepartRebar == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        try{
            return this.steelplantSettingService.saveUsepartRebar(steelplantUsepartRebar, beamId, userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteUsepartRebar", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteUsepartRebar(HttpServletRequest request, @RequestParam("id") String id){
        if(StringUtils.isEmpty(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return this.steelplantSettingService.deleteUsepartRebar(id);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }

    @RequestMapping("/getRebar")
    @ResponseBody
    public ApiResult getRebar(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        return suppliesService.getSuppliesByType2(beamId, "020104");
    }
}
